import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * @Author jlj
 * @Date 2020/12/10 14:55
 * @Description
 * @Modified
 */
public class Geojson2Wkt {


    /**
     * 坐标转换为WKT
     *
     * @author
     * @sate 2019-02-21 10:06
     * @version 1.0
     */
    public static String convert2Wkt(String type, String coordinates) {

        //"Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon"
        StringBuffer wkt = new StringBuffer();

        JSONArray jsonArray = JSONObject.parseArray(coordinates);

        type = type.toUpperCase();
        switch (type) {
            case "POINT":
                //[100.0, 0.0]
                wkt.append("POINT(").append(jsonArray.getString(0)).append(" ")
                        .append(jsonArray.getString(1)).append(")");
                break;
            case "MULTIPOINT":
                //[
                //        [100.0, 0.0],
                //        [101.0, 1.0]
                //    ]
                wkt.append("MULTIPOINT(");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONArray point = jsonArray.getJSONArray(i);
                    wkt.append(point.getString(0)).append(" ").append(point.get(1)).append(",");
                }
                wkt.deleteCharAt(wkt.length() - 1);
                wkt.append(")");
                break;
            case "LINESTRING":
                //和multipoint结构一样
                //[
                //                [101.0, 0.0],
                //                [102.0, 1.0]
                //            ]
                wkt.append("LINESTRING").append(wktLineString(jsonArray));
                break;
            case "MULTILINESTRING":
                //[
                //        [
                //            [100.0, 0.0],
                //            [101.0, 1.0]
                //        ],
                //        [
                //            [102.0, 2.0],
                //            [103.0, 3.0]
                //        ]
                //    ]
                wkt.append("MULTILINESTRING(");
                for (int i = 0; i < jsonArray.size(); i++) {
                    wkt.append("").append(wktLineString(jsonArray.getJSONArray(i))).append("").append(",");
                }
                wkt.deleteCharAt(wkt.length() - 1);
                wkt.append(")");
                break;
            case "POLYGON":
                //POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))
                //"coordinates": [
                //            [
                //                [-180.0, 10.0],
                //                [20.0, 90.0],
                //                [180.0, -5.0],
                //                [-30.0, -90.0]
                //            ]
                //        ]
                wkt.append("POLYGON").append(wktPolygon(jsonArray));
                break;
            case "MULTIPOLYGON":
                //解析为多个polygon
                wkt.append("MULTIPOLYGON(");
                for (int i = 0; i < jsonArray.size(); i++) {
                    wkt.append(wktPolygon(jsonArray.getJSONArray(i))).append(",");
                }
                wkt.deleteCharAt(wkt.length() - 1);
                wkt.append(")");
                break;
        }
        return wkt.toString();
    }

    private static String wktLineString(JSONArray jsonArray) {

        StringBuffer wkt = new StringBuffer();

        wkt.append("(");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONArray point = jsonArray.getJSONArray(i);
            wkt.append(point.getString(0)).append(" ").append(point.get(1)).append(",");
        }
        wkt.deleteCharAt(wkt.length() - 1);
        wkt.append(")");

        return wkt.toString();
    }

    private static String wktPolygon(JSONArray jsonArray) {

        StringBuffer wkt = new StringBuffer();

        wkt.append("(");
        for (int i = 0; i < jsonArray.size(); i++) {
            //是一组组的LineString
            wkt.append("(");
            JSONArray lineString = jsonArray.getJSONArray(i);
            for (int j = 0; j < lineString.size(); j++) {
                JSONArray pointArray = lineString.getJSONArray(j);
                wkt.append(pointArray.getString(0)).append(" ").append(pointArray.getString(1))
                        .append(",");
            }
            if (wkt.length() > 0) {
                //删除最后一个逗号
                wkt.deleteCharAt(wkt.length() - 1);
            }
            wkt.append("),");
        }
        if (wkt.length() > 0) {
            wkt.deleteCharAt(wkt.length() - 1);
        }
        wkt.append(")");

        return wkt.toString();
    }

    public static void main(String[] args) {
        String polygon_Coords="[ [ [ 116.816194787717265, 39.938504427401945 ], [ 117.18294343775753, 40.127178588405322 ], " +
                "[ 117.348298320434651, 40.050860950246651 ], [ 117.403416614660358, 39.872786461209756 ], " +
                "[ 117.212622519263675, 39.724391053679007 ], [ 116.917951638595483, 39.722271119285715 ], " +
                "[ 116.816194787717265, 39.938504427401945 ] ] ]";
        String polygon_Coords2="[ [ [ 117.132774820158545, 40.063813600327968 ], [ 117.04702320790922, 39.958137348761788 ], [ 116.981650214100256, 39.904870464917451 ], [ 116.904171110326672, 39.880658244988204 ], [ 116.786874, 39.886963 ], [ 116.743765153295399, 40.046057972379856 ], [ 116.816401813083118, 40.20908691990342 ], [ 116.987501500583093, 40.268810395728885 ], [ 117.131160672163261, 40.225228399856249 ], [ 117.132774820158545, 40.063813600327968 ] ] ]";
        String point_Coord="[117.13277482015855,40.06381360032797]";
        String wkt = convert2Wkt("POLYGON", polygon_Coords);
        String wktPolygon2=convert2Wkt("POLYGON",polygon_Coords2);
        String wktPoint=convert2Wkt("POINT",point_Coord);
        System.out.println("Polygon-1:"+wkt);
        System.out.println("Polygon-2:" + wktPolygon2);
        System.out.println("Point:"+wktPoint);
    }
}
